
//
//  VoiceButtonView.m
//  KeyBoard
//
//  Created by ShaoFeng on 16/8/18.
//  Copyright © 2016年 Cocav. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define CustomBackGroundColor [UIColor colorWithRed:243 / 255.0 green:243 / 255.0 blue:243 / 255.0 alpha:1]

#import "VoiceButtonView.h"
#import "UIView+Extension.h"
#import "RecorderTool.h"

@interface VoiceButtonView ()

@property (nonatomic,strong) UIButton *recordButton;
@property (nonatomic,strong) RecorderTool *recorderTool;
@end

@implementation VoiceButtonView

-(RecorderTool *)recorderTool
{
    if (!_recorderTool) {
        self.recorderTool = [[RecorderTool alloc] init];
    }
    
    return _recorderTool;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = CustomBackGroundColor;
        
        [self layoutUI];
    }
    return self;
}

- (void)layoutUI
{
    self.recordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.recordButton setImage:[UIImage imageNamed:@"mianView"] forState:UIControlStateNormal];
    self.recordButton.size = CGSizeMake(100, 100);
    self.recordButton.center = self.center;
    [self addSubview:self.recordButton];
    
    [self.recordButton addTarget:self action:@selector(begineRecord:) forControlEvents:UIControlEventTouchDown];
    [self.recordButton addTarget:self action:@selector(completeRecord:) forControlEvents:UIControlEventTouchUpInside];
    [self.recordButton addTarget:self action:@selector(completeRecord:) forControlEvents:UIControlEventTouchCancel];
    [self.recordButton addTarget:self action:@selector(beginCancelRecord:) forControlEvents:UIControlEventTouchDragOutside];
    [self.recordButton addTarget:self action:@selector(cancelRecord:) forControlEvents:UIControlEventTouchUpOutside];
    [self.recordButton addTarget:self action:@selector(stopCancelRecord:) forControlEvents:UIControlEventTouchDragInside];
    
}

#pragma mark -- 开始录音
- (void)begineRecord:(UIButton *)button
{
    NSLog(@"开始录音");
    self.recorderTool.parentView = self.parentView;
    NSString *path = [self filePath];
    [self.recorderTool startRecordWithPath:path];
}
#pragma mark -- 完成录音/录音被中断
- (void)completeRecord:(UIButton *)button
{
     NSLog(@"完成录音/录音被中断");
    [_recorderTool stopRecordWithCompletion:^(BOOL succeed, NSString *recordTime, NSString *recordPath) {
        if (succeed) {
            NSLog(@"录音完成的路径：%@-----录音时间：%@",recordPath,recordTime);
        } else {
            NSLog(@"录音失败");
        }
    }];
}
#pragma mark -- 准备取消录音
- (void)beginCancelRecord:(UIButton *)button
{
     NSLog(@"准备取消录音");
    RecordStatus status = RecordStatusCancel;
    [[NSNotificationCenter defaultCenter] postNotificationName:RECORD_STATUS_NOTIFICATION object:@(status)];
}
#pragma mark -- 取消录音
- (void)cancelRecord:(UIButton *)button
{
    NSLog(@"取消录音");
    [_recorderTool deleteRecordWithCompletion:^(BOOL succeed) {
        if (succeed) {
            NSLog(@"cancel record success!");
        }
    }];
}

#pragma mark -- 停止取消录音
- (void)stopCancelRecord:(UIButton *)button
{
     NSLog(@"停止取消录音");
    RecordStatus status = RecordStatusNormal;
    [[NSNotificationCenter defaultCenter] postNotificationName:RECORD_STATUS_NOTIFICATION object:@(status)];
}


#pragma --mark 录音的路径
- (NSString *)filePath
{
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"PMRecord.caf"];
    return filePath;
}
@end
